package com.book.ordermanagement.listener;

import com.alibaba.fastjson.JSON;
import com.book.ordermanagement.command.OrderCommand;
import com.book.ordermanagement.command.invoker.OrderCommandInvoker;
import com.book.ordermanagement.state.OrderStateChangeAction;
import com.book.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component;

import java.util.Objects;

import static com.book.ordermanagement.state.OrderStateConstant.*;

@Component
@WithStateMachine(name = "orderStateMachine")
public class OrderStateListener {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OrderCommand orderCommand;

    @OnTransition(source = ORDER_WAIT_PAY,target = ORDER_WAIT_SEND)
    public boolean payToSend(Message<OrderStateChangeAction> message){
        Order order = (Order) message.getHeaders().get("order");
        assert order != null;
        if (!Objects.equals(order.getOrderState(), ORDER_WAIT_PAY)) {
            throw new UnsupportedOperationException("Order state error");
        }
        // 支付成功后修改状态为待发货，并更新redis缓存
        order.setOrderState(ORDER_WAIT_SEND);
        redisTemplate.opsForValue().set(order.getOrderId(), JSON.toJSONString(order));
        OrderCommandInvoker invoker = new OrderCommandInvoker();
        invoker.invoke(orderCommand,order);
        return true;

    }

    @OnTransition(source = ORDER_WAIT_SEND,target = ORDER_WAIT_RECEIVE)
    public boolean sendToReceive(Message<OrderStateChangeAction> message){
        Order order = (Order) message.getHeaders().get("order");
        assert order != null;
        if (!Objects.equals(order.getOrderState(), ORDER_WAIT_SEND)) {
            throw new UnsupportedOperationException("Order state error");
        }
        // 支付成功后修改状态为待发货，并更新redis缓存
        order.setOrderState( ORDER_WAIT_RECEIVE);
        redisTemplate.opsForValue().set(order.getOrderId(), JSON.toJSONString(order));
        OrderCommandInvoker invoker = new OrderCommandInvoker();
        invoker.invoke(orderCommand,order);
        return true;

    }

    @OnTransition(source = ORDER_WAIT_RECEIVE,target = ORDER_FINISH)
    public boolean receiveToFinish(Message<OrderStateChangeAction> message){
        Order order = (Order) message.getHeaders().get("order");
        assert order != null;
        if (!Objects.equals(order.getOrderState(), ORDER_WAIT_RECEIVE)) {
            throw new UnsupportedOperationException("Order state error");
        }
        // 支付成功后修改状态为待发货，并更新redis缓存
        order.setOrderState( ORDER_FINISH);
        redisTemplate.delete(order.getOrderId());
        redisTemplate.delete(order.getOrderId()+"STATE");
        OrderCommandInvoker invoker = new OrderCommandInvoker();
        invoker.invoke(orderCommand,order);
        return true;

    }
}
